package com.hsnn.medstgmini.trading.pur.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.form.DicAreaForm;
import com.hsnn.medstgmini.base.dict.service.DicAreaManager;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.model.UserType;
import com.hsnn.medstgmini.trading.pur.model.OrderPackStatus;
import com.hsnn.medstgmini.trading.pur.model.form.OrderPackage;
import com.hsnn.medstgmini.trading.pur.service.OrderAlreadyManager;
import com.hsnn.medstgmini.trading.pur.service.OrderConfirmManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 已配送订单
 * @author 蔡春龙
 * @date 2015年7月15日
 */
@Controller
@RequestMapping("/orderAlready")
public class OrderAlreadyController {

	private static final Logger log = Logger
			.getLogger(OrderAlreadyController.class);


	@Autowired
	private OrderAlreadyManager orderAlreadyManager;

	@Autowired
	private OrderConfirmManager orderConfirmManager;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;
	
	@Autowired
	private DicAreaManager dicAreaManager;
	
	@Value("#{sysProp['system.province']}")
	private String province;

	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	public SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}

	/**
	 * @category 进入已配送订单页面
	 * @date 2015年7月15日
	 * @param model
	 * @return
	 */
	@RequestMapping("/toOrderAlready")
	public String toOrderAlready(Model model) {
		log.info("enter toCreateOrder");
		DicAreaForm area = dicAreaManager.getDicAreaById(province);
		request.setAttribute("area", area);
		return "/trading/pur/orderAlready";
	}

	/**
	 * @category 查询已配送订单
	 * @date 2015年7月15日
	 * @param address
	 * @return
	 */
	@RequestMapping("/getOrderAlready")
	@ResponseBody
	public Pagination getOrderAlready(OrderPackage orderPackage) {
		log.info("enter getOrderAlready method");
		Pagination page = new Pagination(request);
		try {
			if (orderPackage == null) {
				orderPackage = new OrderPackage();
			}
			SysUser user = getUser();
			// 获取当前登陆配送企业id
			if (user.getType() == UserType.ps.getKey()) {
				StdCompinfo comp = user.getInfo();
				if (comp != null) {
					orderPackage.setDelCompId(comp.getCompId());
				}
			}
			orderAlreadyManager.getOrderAlready(orderPackage, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 进入未配送订单明细页面
	 * @date 2015年7月15日
	 * @param model
	 * @param orderPackId
	 * @return
	 */
	@RequestMapping("/toWpsOrderDetail")
	public String toWpsOrder(Model model, String orderPackId) {
		model.addAttribute("orderPackId", orderPackId);
		return "/trading/pur/wpsOrderDetail";
	}

	/**
	 * @category 查询未配送订单明细
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @return
	 */
	@RequestMapping("/getWpsOrderDetail")
	@ResponseBody
	public Pagination getWpsOrderDetail(OrderPackage orderPackage) {
		log.info("enter getWpsOrderDetail method");
		Pagination page = new Pagination(request);
		try {
			if (orderPackage == null) {
				orderPackage = new OrderPackage();
			}
			orderPackage.setOrderStatus(OrderPackStatus.confirmed.getKey());
			SysUser user = getUser();
			// 获取当前登陆配送企业id
			if (user.getType() == UserType.ps.getKey()) {
				StdCompinfo comp = user.getInfo();
				if (comp != null) {
					orderPackage.setDelCompId(comp.getCompId());
				}
			}
			orderConfirmManager.getOrderConfirmDetail(orderPackage, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 进入已配送订单明细页面
	 * @date 2015年7月15日
	 * @param model
	 * @param orderPackId
	 * @return
	 */
	@RequestMapping("/toYpsOrderDetail")
	public String toYpsOrderDetail(Model model, String orderPackId) {
		model.addAttribute("orderPackId", orderPackId);
		return "/trading/pur/ypsOrderDetail";
	}

	/**
	 * @category 查询已配送订单明细
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @return
	 */
	@RequestMapping("/getYpsOrderDetail")
	@ResponseBody
	public Pagination getYpsOrderDetail(OrderPackage orderPackage) {
		log.info("enter getYpsOrderDetail method");
		Pagination page = new Pagination(request);
		try {
			if (orderPackage == null) {
				orderPackage = new OrderPackage();
			}
			// orderPackage.setOrderStatus(OrderPackStatus.dispatched.getKey());
			SysUser user = getUser();
			// 获取当前登陆配送企业id
			if (user.getType() == UserType.ps.getKey()) {
				StdCompinfo comp = user.getInfo();
				if (comp != null) {
					orderPackage.setDelCompId(comp.getCompId());
				}
			}
			orderConfirmManager.getOrderConfirmDetail(orderPackage, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 进入无法配送订单明细页面
	 * @date 2015年7月15日
	 * @param model
	 * @param orderPackId
	 * @return
	 */
	@RequestMapping("/toWfpsOrderDetail")
	public String toWfpsOrderDetail(Model model, String orderPackId) {
		model.addAttribute("orderPackId", orderPackId);
		return "/trading/pur/wfpsOrderDetail";
	}

	/**
	 * @category 查询无法配送订单明细
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @return
	 */
	@RequestMapping("/getWfpsOrderDetail")
	@ResponseBody
	public Pagination getWfpsOrderDetail(OrderPackage orderPackage) {
		log.info("enter getWfpsOrderDetail method");
		Pagination page = new Pagination(request);
		try {
			if (orderPackage == null) {
				orderPackage = new OrderPackage();
			}
			// orderPackage.setOrderStatus(OrderPackStatus.unable.getKey());
			SysUser user = getUser();
			// 获取当前登陆配送企业id
			if (user.getType() == UserType.ps.getKey()) {
				StdCompinfo comp = user.getInfo();
				if (comp != null) {
					orderPackage.setDelCompId(comp.getCompId());
				}
			}
			orderConfirmManager.getOrderConfirmDetail(orderPackage, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

}
